<?php

namespace app\home\controller;

use app\home\lib\MyController;
use app\index\controller\WeiXinPay;

class User extends MyController
{
	public function index(){
		$page = input('page');
		$search = input('search');

		if (!isset($page) || $page < 1) {
			$page = 1;
		}
		$condition = array();
		if(!empty($search)){
			$condition['nick_name|phone']=array("like","%$search%");
		}
		//$condition['status']=array('in','1,2');
		$count = db('user_info')->where($condition)->count();
		$allPage = ceil($count / 10);
		if ($page > $allPage) {
			$page = $allPage;
		}
		$res=db('user_info')->where($condition)->page($page, 10)->order('last_login desc')->select();
		$res=array_map(function($value){
			$value['collection_num_qa'] = db('user_collection')->where('openid',$value['openid'])->where('type','qa')->where('status',1)->count();
			$value['collection_num_muse'] = db('user_collection')->where('openid',$value['openid'])->where('type','muse')->where('status',1)->count();
			$value['collection_num_subject'] = db('user_collection')->where('openid',$value['openid'])->where('type','subject')->where('status',1)->count();
			$value['limit_status'] = ($value['limit_time']>date("Y-m-d H:i:s"))?'封禁':'正常';
			return $value;
		},$res);

		//总用户数
		$statistics['count'] = $count;
		//今日访问用户数
		$statistics['today_new_num'] = db('user_info')->where('last_login','>',date('Y-m-d'))->count();
		//今日新增用户数
		$statistics['today_add_num'] = db('user_info')->where('create_time','>',date('Y-m-d'))->count();

		$this->assign('page', $page);
		$this->assign('count', $count);
		$this->assign('allpage', $allPage);
		$this->assign('search',$search);
		$this->assign('statistics',$statistics);
		$this->assign('res', $res);
		return $this->fetch();
	}

	public function add(){
		$id = input('id');
		$page = input('page');
		$search=input('search');

		if (!empty($id)) {
			$user = db('user_info')->where('id',$id)->find();
			$this->assign('res', $user);
		}

		$this->assign('page', $page);
		$this->assign('search',$search);
		return $this->fetch();
	}

	public function edit(){
		$data = input('post.');
		$page = input('page');
		$search=input('search');

		if(empty($data['nick_name'])){
			$this->error('数据为空！');
		}

		if(empty($data['birthday'])){
			unset($data['birthday']);
		}

		$id = $data['id'];
		unset($data['id']);
		if(empty($id)){
			if(empty($data['avatar_img'])){
				$this->error('未传头像！');
			}
			$res=db('user_info')->insert($data);
		}else{
			$res = db('user_info')->where('id',$id)->update($data);
		}
		if($res){
			$this->success('操作成功！',"index?page={$page}&search={$search}");
		}else{
			$this->error('操作失败！',"index?page={$page}&search={$search}");
		}
	}

	public function delete(){
		$id = input('id');
		$page = input('page');
		$status = input('status');
		$search=input('search');

		if(empty($id)){
			$this->error('数据为空！');
		}

		if($status==1 || $status==2){
			$status=0;
		}else{
			$status=2;
		}
		$res = db('user_info')->where('id',$id)->update(array('status'=>$status));
		if ($res) {
			$this->success('操作成功！',"index?page={$page}&search={$search}");
		} else {
			$this->error('操作失败！',"index?page={$page}&search={$search}");
		}
	}

	// 
	public function statistics(){
		//用户id
		$id = input('id');
		$page = input('page');
		$search=input('search');
		//返回用户列表参数
		$i_page = input('i_page');
		$i_search = input('i_search');

		if (!isset($page) || $page < 1) {
			$page = 1;
		}
		$limit = 10;
		$tmp_where=array('u.id'=>$id);
		if(!empty($search)){
			$tmp_where['a.transaction_id']=array("like","%$search%");
		}
		$count = db('as_order a')
			->join('user_info u', 'a.openid=u.openid')
			->join('qa_main q', 'a.qa_main_id=q.id')
			->where($tmp_where)
			->count();
		$allPage = ceil($count / $limit);
		if ($page > $allPage && $allPage > 0) {
			$page = $allPage;
		}
		$res = db('as_order a')
			->field('a.id,a.openid,a.qa_main_id,a.qa_main_id,a.transaction_id,a.coupon_id,a.coupon_from_openid,a.cp_status,a.cash_out_id_m,a.cash_out_id_o,a.cash_out_stop,a.pay_num,a.total_fee,a.stage,a.status,a.create_time,'
					.'u.nick_name,q.title,q.type')
			->join('user_info u', 'a.openid=u.openid')
			->join('qa_main q', 'a.qa_main_id=q.id')
			->where($tmp_where)
			->page($page,$limit)
			->order('a.create_time desc')
			->select();

		//总参与次数
		$statistics['count'] = db('as_order a')
			->join('user_info u', 'a.openid=u.openid')
			->join('qa_main q', 'a.qa_main_id=q.id')
			->where('u.id',$id)
			->count();

		//总消费
		$statistics['all_outlay'] = db('as_order a')
			->join('user_info u', 'a.openid=u.openid')
			->join('qa_main q', 'a.qa_main_id=q.id')
			->where('u.id',$id)
			->sum('a.total_fee');

		$this->assign('i_page', $i_page);
		$this->assign('i_search',$i_search);
		$this->assign('id', $id);
		$this->assign('page', $page);
		$this->assign('count', $count);
		$this->assign('allpage', $allPage);
		$this->assign('search',$search);
		$this->assign('statistics',$statistics);
		$this->assign('res',$res);
		return $this->fetch();
	}

	public function refund(){
		$id = input('id');
		$u_id = input('u_id');
		$page = input('page');
		$search=input('search');
		//返回问答列表参数
		$i_page = input('i_page');
		$i_search = input('i_search');
		$tiaozhuan = "id=".$u_id."&page=".$page."&search=".$search."&i_page=".$i_page."&i_search=".$i_search;

		$asOrder = db('as_order')->where('id',$id)->find();
		if (!empty($asOrder) && $asOrder['total_fee']>=0 && $asOrder['cash_out_id_m']<1 && $asOrder['cash_out_id_o']<1) {
			//判断是否在账期前
            $is_refund = common_refund($id);
			if($is_refund){
				if($asOrder['transaction_id']){
					$weiXinPay = new WeiXinPay();
					$rt = $weiXinPay->toRefund($is_refund, "平台主动发起退款"); // 微信退款
					if ($rt != "SUCCESS") {
						$this->error("退款失败，请稍后再试！","statistics?".$tiaozhuan);
					}
				}
				db('as_order')->where('id',$id)->update(array('cp_status'=>-6));
				// 票卷和问答数量加回去
				db("qa_circle")->where("id = {$asOrder['qa_main_id']}")->inc('qa_surplus,cp_surplus', $asOrder['pay_num'])->update();
				$this->success("退款成功！","statistics?".$tiaozhuan);
			}else{
				$this->error("不符合退款要求！","statistics?".$tiaozhuan);
			}
		}else{
			$this->error("不符合退款要求！","statistics?".$tiaozhuan);
		}
	}

	public function refund_more(){
		if(empty(input('id'))) return server_response(1,'未选择退款订单！');

		$success_num = 0;
		$id = substr(input('id'),0,strrpos(input('id'),','));
		foreach (explode(',',$id) as $v){
			$asOrder = db('as_order')->where('id',$v)->find();
			if (!empty($asOrder) && $asOrder['total_fee']>=0 && $asOrder['cash_out_id_m']<1 && $asOrder['cash_out_id_o']<1) {
				//判断是否在账期前
                $is_refund = common_refund($v);
				if($is_refund){
					if($asOrder['transaction_id']){
						$weiXinPay = new WeiXinPay();
						$rt = $weiXinPay->toRefund($is_refund, "平台主动发起退款"); // 微信退款
						if ($rt != "SUCCESS") {
							continue;
						}
					}
					db('as_order')->where('id',$v)->update(array('cp_status'=>-6));
					// 票卷和问答数量加回去
					db("qa_circle")->where("id = {$asOrder['qa_main_id']}")->inc('qa_surplus,cp_surplus', $asOrder['pay_num'])->update();
					++$success_num;
				}else{
					continue;
				}
			}else{
				continue;
			}
		}
		return server_response(0,'退款成功！',array('num'=>$success_num));
	}

	public function cash_out_stop(){
		$id = input('id');
		$u_id = input('u_id');
		$cash_out_stop = input('cash_out_stop');
		$page = input('page');
		$search=input('search');
		//返回问答列表参数
		$i_page = input('i_page');
		$i_search = input('i_search');
		$tiaozhuan = "id=".$u_id."&page=".$page."&search=".$search."&i_page=".$i_page."&i_search=".$i_search;

		$asOrder = db('as_order')->where('id',$id)->find();
		if (!empty($asOrder) && $asOrder['total_fee']>0 && $asOrder['cash_out_id_m']<1 && $asOrder['cash_out_id_o']<1 && $asOrder['transaction_id']) {
			//判断是否在账期前
            $is_cash_out_stop = common_refund($id);
			if($is_cash_out_stop){
				$cash_out_stop_status = ($cash_out_stop)==1?'0':'1';
				db('as_order')->where('id',$id)->update(array('cash_out_stop'=>$cash_out_stop_status));
				$this->success("阻断成功！","statistics?".$tiaozhuan);
			}else{
				$this->error("不符合阻断要求！","statistics?".$tiaozhuan);
			}
		}else{
			$this->error("不符合阻断要求！","statistics?".$tiaozhuan);
		}
	}

	public function account(){
		$page = input('page');
		$search = input('search');

		if (!isset($page) || $page < 1) {
			$page = 1;
		}
		$condition = array();
		if(!empty($search)){
			$condition['name']=array("like","%$search%");
		}
		//$condition['status']=array('in','1,2');
		$count = db('account')->where($condition)->count();
		$allPage = ceil($count / 10);
		if ($page > $allPage) {
			$page = $allPage;
		}
		$res=db('account')->where($condition)->page($page, 10)->order('class,status desc,id desc')->select();

		$this->assign('page', $page);
		$this->assign('count', $count);
		$this->assign('allpage', $allPage);
		$this->assign('search',$search);
		$this->assign('res', $res);
		return $this->fetch();
	}

	public function account_add(){
		$id = input('id');
		$page = input('page');
		$search=input('search');

		if (!empty($id)) {
			$user = db('account')->where('id',$id)->find();
			$this->assign('res', $user);
		}

		$this->assign('page', $page);
		$this->assign('search',$search);
		return $this->fetch();
	}

	public function account_edit(){
		$data = input('post.');
		$page = input('page');
		$search=input('search');

		if(empty($data['name']) || empty($data['password'])){
			$this->error('数据为空！');
		}

		$id = $data['id'];
		unset($data['id']);
		if(empty($id)){
			$res=db('account')->insert($data);
		}else{
			$res = db('account')->where('id',$id)->update($data);
		}
		if($res){
			$this->success('操作成功！',"account?page={$page}&search={$search}");
		}else{
			$this->error('操作失败！',"account?page={$page}&search={$search}");
		}
	}
}
